<template>
  <el-form :model="goods.subsign" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <el-form-item label="所属分类" prop="region">
      <el-select v-model="goods.subsign.category_id" placeholder="请选择所属分类">
        <el-option
          :label="item.name"
          :value="item.id"
          v-for="item in goods.categoryList"
          :key="item.id"
        ></el-option>
      </el-select>
    </el-form-item>

    <el-form-item label="商品名称" prop="name">
      <el-input v-model="goods.subsign.name"></el-input>
    </el-form-item>

    <el-form-item label="所属品牌" prop="region">
      <el-select v-model="goods.subsign.brand_id" placeholder="请选择品牌">
        <el-option
          :label="item.name"
          :value="item.id"
          v-for="item in goods.brandList"
          :key="item.id"
        ></el-option>
      </el-select>
    </el-form-item>

    <el-form-item label="商品简介" prop="desc">
      <el-input type="textarea" v-model="goods.subsign.goods_brief"></el-input>
    </el-form-item>

    <el-form-item label="商品图片">
      <el-upload
        class="avatar-uploader"
        action
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :before-upload="beforeAvatarUpload"
        :http-request="uploadFile"
      >
        <img v-if="goods.subsign.list_pic_url" :src="goods.subsign.list_pic_url" class="avatar" />
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
      <div class="addput011">推荐图片尺寸：750*420</div>
    </el-form-item>

    <el-form-item label="轮播图图片">
      <el-upload
        :file-list="goods.subsign.gallery"
        :on-remove="removeFunction"
        action
        list-type="picture-card"
        :http-request="uploadFiles"
      >
        <i class="el-icon-plus"></i>
      </el-upload>
      <!-- <el-dialog v-if="goods.subsign.gallery.length">
        <img width="100%" />
      </el-dialog>-->
    </el-form-item>

    <!-- 当前位置 -->
    <el-form-item label="商品单位">
      <el-input v-model="goods.subsign.goods_unit"></el-input>
    </el-form-item>
    <el-form-item label="商品库存">
      <el-input v-model="goods.subsign.goods_number"></el-input>
    </el-form-item>
    <el-form-item label="商品售价">
      <el-input v-model="goods.subsign.retail_price"></el-input>
    </el-form-item>

    <el-form-item label="推荐类型" prop="type">
      <el-checkbox label="新品" v-model="goods.subsign.is_new" name="type"></el-checkbox>
      <el-checkbox label="人气" v-model="goods.subsign.is_hot" name="type"></el-checkbox>
    </el-form-item>

    <el-form-item label="上架" prop="delivery">
      <el-switch v-model="goods.subsign.is_delete"></el-switch>
    </el-form-item>

    <el-form-item label="排序" prop="name">
      <el-input v-model="goods.subsign.sort_order"></el-input>
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm')">确认保存</el-button>
      <el-button @click="resetForm('ruleForm')">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
//引入vuex
import { mapState } from "vuex";
export default {
  methods: {
    removeFunction(fileList) {
      // console.log(fileList.url)
      this.$store.commit("goodschangegallerys", fileList.url);
    },
    handleAvatarSuccess() {},
    // 上传单张图片
    uploadFile(item) {
      console.log("单张图片", item);
      let form = new FormData();
      form.append("good_pic", item.file); // 发送请求
      this.$store.dispatch("goodsPutimg", form);
      console.log("查看form", form);
    },
    //上传轮播图图片
    uploadFiles(item) {
      console.log("轮播图图片", item);
      let form = new FormData();
      form.append("good_pic", item.file); // 发送请求
      this.$store.dispatch("goodsPutimgs", form);
      // console.log("查看form", form);
    },
    beforeAvatarUpload() {},

    submitForm() {
      // console.log("查看参数前", this.goods.subsign);

      //修改本地vuex
      this.$store.commit("goodsGetvalue", this.goods.subsign);
      //发起请求
      this.$store.dispatch("goodsAddgoods");
      console.log("查看参数", this.goods.subsign);
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  created() {},
  computed: {
    ...mapState(["goods"])
  }
};
</script>

<style lang="less" scoped>
.addput011 {
  font-size: 12px;
  color: #999;
}

.el-select-dropdown__item.selected {
  color: #606266;
  font-weight: normal;
}
.el-input,
.el-textarea {
  width: 220px;
}

/deep/.avatar-uploader {
  border: 1px solid #d9d9d9;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 187px;
  height: 105px;
}

/deep/.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
/deep/.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 105px;
  text-align: center;
}

/deep/.avatar {
  width: 187px;
  height: 105px;
  display: block;
}

//轮播图样式
/deep/.avatar-uploaders {
  width: 178px;
  height: 178px;
  border: 1px solid #d9d9d9;
  cursor: pointer;
  position: relative;
  display: inline-block;
  //   overflow: hidden;
}
/deep/.avatars {
  width: 178px;
  height: 178px;
  display: block;
}
/deep/.avatar-uploader-icons {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
</style>